package com.controller;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.domain.EMailMessage;

public class EMailSender {
	
	public void sendEmail(EMailMessage message){
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props,null);
		Message sendingMessage;
		try {
			sendingMessage = createMessage(message,session);
			Transport.send(sendingMessage);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private Message createMessage(EMailMessage message,Session session) throws UnsupportedEncodingException, MessagingException{
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("whereiwastest@appspot.gserviceaccount.com","WhereIWas.com Admin"));
		msg.addRecipient(Message.RecipientType.TO, new InternetAddress(message.getMailDestination(),"Mr. " + message.getMailDestination()));
		msg.setSubject(message.getMailSubject());
		msg.setText(message.getMessageBody());
		return msg;
	}
}
